package cn.sypt.yuhao.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.mail.Mail;
import cn.itcast.mail.MailUtils;
import cn.sypt.yuhao.domain.User;
import cn.sypt.yuhao.model.JavaMail;
import cn.sypt.yuhao.model.Tool;
import cn.sypt.yuhao.model.UserBean;
import cn.sypt.yuhao.model.UserBeanBO;
import cn.sypt.yuhao.model.Uuid;

public class LoginsyCl extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		//得到关于用户的业务处理类
		UserBeanBO usb=new UserBeanBO();
		
		ArrayList al=new ArrayList();
		//数据库的返回boolean型变量
		boolean bl;
		
		//页面动作请求类型
		String flag=request.getParameter("flag");
		
		//用户的javabean
		User ub=new User();
		
		//页面信息变量
		String msg="";
		
		//取出登录用户的信息
		ub=(User)request.getSession().getAttribute("user");
		
		
		if(flag.equals("login"))
		{
			
			if(ub==null)
			{
				
			String uname=request.getParameter("uname");
			String upass=request.getParameter("upass");
			
			bl=usb.checkUser(uname, upass);
			
			if(bl)
			{
				
				ub=usb.getUser(uname);
				//将成功登录的信息放入session
				request.getSession().setAttribute("user", ub);
				System.out.println("uname="+uname+",upass="+upass);
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}else
			{
				msg="对不起!用户名或密码错误";
				request.setAttribute("msg", msg);
				request.getRequestDispatcher("ui/login.jsp").forward(request, response);
			}
			}else
			{
				msg="您已经登录过,请不要重复登录";
				request.setAttribute("msg", msg);
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}
		}else if(flag.equals("zhuce"))
		{
		
			String uname=request.getParameter("uname");
			String upass=request.getParameter("upass");
			String uemail=request.getParameter("uemail");
			String uaddress=request.getParameter("uaddress");
			String ubirth=request.getParameter("ubirth");
			String uphone=request.getParameter("uphone");
			String uid=new Uuid().uuid();
			/////邮箱验证
			
			/*
			 * 登录邮件服务器，得到session
			 */
			String host = "smtp.163.com";//服务器主机名
			String name = "experimentlearning@163.com";//登录名
			String pass = "learning2016";//登录密码
			Session session = MailUtils.createSession(host, name, pass);
			
			/*
			 * 创建Mail对象
			 */
			String from = "experimentlearning@163.com";
			String to = uemail;
			String subject ="来自高校实验学习平台的激活邮件";
			String content ="尊敬的会员:"+uname+"您好，欢迎您注册高校实验学习平台，请点击链接：http://localhost:8080/sypt/CategoryServlet？method=findAll   激活您的账户(如果该链接无法点击，请将该链接复制到浏览器的地址栏中进行激活，该邮件在48小时内有效，48小时之后需要重新注册激活)。高校实验学习平台，专注于初中，高中各门学科的实验，在这里，您可以找到自己想要的实验，以帮助您更高效率的学习";
			Mail mail = new Mail(from, to, subject, content);
			/*
			 * 发送邮件
			 */
			try {
				MailUtils.send(session, mail);
			} catch (MessagingException e) {
				throw new RuntimeException(e);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
			
			
			
		/////邮箱验证
			
			bl=usb.zhuce(uid, uname, upass, uemail,uaddress,ubirth,upass);
			
			
			if(bl)
			{
				
				msg="注册成功!";
				request.setAttribute("msg", msg);
				request.getRequestDispatcher("ui/login.jsp").forward(request, response);
			}else
			{
				msg="注册失败!";
				request.setAttribute("msg", msg);
				request.getRequestDispatcher("ui/zhuce.jsp").forward(request, response);
			}
			
		}else if(flag.equals("logout"))
		{
			
			msg="注销成功!";
			request.setAttribute("msg", msg);
			request.getSession(true).invalidate();
			response.sendRedirect("index.jsp");
		}else if(flag.equals("grgl"))
		{
			
			if(ub==null)
			{
				msg="请先登录!";
				request.setAttribute("msg", msg);
				request.getRequestDispatcher("ui/login.jsp").forward(request, response);
			}else
			{
				request.getRequestDispatcher("home/pshp.jsp").forward(request, response);
			}
		}else if(flag.equals("search"))
		{
			
			String search=request.getParameter("search");
			al=usb.shouYeSearch(search);
			for(int i=0;i<al.size();i++)
			{
				System.out.println((String)al.get(i));
			}
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}
		else if(flag.equals("wjmm1"))
		{
			String uname=request.getParameter("uname");
			User user=usb.getUser(uname);
			String uemail=user.getUemail();
			System.out.println(uemail);
			if(uemail!=null)
			{
				request.setAttribute("uemail", uemail);
				request.setAttribute("uname", uname);
			request.getRequestDispatcher("/ui/wjmm2.jsp").forward(request, response);
			}else
			{
				msg="用户不存在!";
				request.setAttribute("msg", msg);
				request.getRequestDispatcher("/ui/wjmm1.jsp").forward(request, response);
			}
		}
		else if(flag.equals("wjmm2"))
		{
			String uemail=request.getParameter("uemail");//获得浏览器传过来的uemail
			String uname=request.getParameter("uname");
			
			User user=usb.getUser(uname);               // 通过用户名查出该用户实体
			if(uemail.equals(user.getUemail()))         //用户输入的邮箱名和数据库中的该用户的邮箱名进行比对
			{
				String upass=user.getUpass();           //获得该用户的密码 
				new JavaMail().send(user.getUemail(), "来自汇实验-实验虚拟品台的忘记密码邮件", "邮箱确认无误，您的密码是"+upass);
				msg="密码已通过邮件发送给您，请登陆后及时修改密码!";
				request.setAttribute("msg", msg);
				request.getRequestDispatcher("/ui/login.jsp").forward(request, response);
			}else
			{
				msg="输入的邮箱与原邮箱不匹配!";
				request.setAttribute("msg", msg);
				request.setAttribute("uemail", user.getUemail());
				request.setAttribute("uname", uname);
				request.getRequestDispatcher("/ui/wjmm2.jsp").forward(request, response);
			}
		}
		 
		
		
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}



}
